import { Config } from '../utils/Config.js';
import { Base } from '../utils/Base.js';

class Address extends Base{
  constructor() {
    super();
  }
  //获取收货地址
  getAddress(callback){
    var that = this;
    var param = {
      url: 'address',
      sCallback: function (data) {
        data = data.data;
        callback && callback(data);
      }
    };
    this.request(param);
  }
  /*
  *根据省市县信息组装地址信息
  * provinceName , province 前者为 微信选择控件返回结果，后者为查询地址时，自己服务器后台返回结果
  */
  setAddressInfo(res) {
    var province = res.provinceName || res.province,
      city = res.cityName || res.city,
      country = res.countyName || res.country,
      detail = res.detailInfo || res.detail;
    var totalDetail = city + country + detail;

    //console.log(res);

    //直辖市，取出省部分
    if (!this.isCenterCity(province)) {
      totalDetail = province + totalDetail;
    };
    return totalDetail;
  }
  /*是否为直辖市*/
  isCenterCity(name) {
    var centerCitys = ['北京市', '天津市', '上海市', '重庆市'],
      flag = centerCitys.indexOf(name) >= 0;
    return flag;
  }
  //保存收获地址导服务器
  submitAddress(data,callback) {
    var that = this;
    var address = that._submitUpdateAddress(data);
    var param = {
      url: 'address',
      type:'post',
      data:address,
      sCallback: function (data) {
        data = data.data;
        callback && callback(data);
      }
    };
    this.request(param);
  }
  //重新组装微信收货地址返回的数据
  _submitUpdateAddress(res,callback){
    var formData = {
      name: res.userName,
      province: res.provinceName,
      city: res.cityName,
      country: res.countyName,
      mobile: res.telNumber,
      detail: res.detailInfo
    };
    return formData;
  }
}
export {Address};